package com.microexam.sunorder.controller;

import com.microexam.suncommon.domain.Order;
import com.microexam.suncommon.util.Constants;
import com.microexam.suncommon.util.ResultVO;
import com.microexam.sunorder.service.IOrderService;
import com.microexam.sunorder.service.IProductService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author gaopeng
 * @create 2020/04/2020/4/8 10:49:26
 */
@RestController
public class OrderController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IOrderService service;

    @Autowired
    private IProductService productService;

    @PostMapping("get")
    @GlobalTransactional
    public ResultVO getAll() {
//        List<Orders> ordersList=new ArrayList<>();
        ResultVO resultVO = new ResultVO(500,"错误");

        List<Order> ordersList = service.getAllOrders();
        if (ordersList != null) {
            System.out.println(ordersList);
            resultVO.setStatus(200);
            resultVO.setMessage("OK");
            resultVO.setData(ordersList);
        }
        return resultVO;
    }



    @PostMapping("/orders/{pid}/{number}")
    @GlobalTransactional
    public ResultVO create(@PathVariable Integer pid, @PathVariable Integer number) {
        try {
            System.out.println("pid=========="+pid+"number==========="+number);
            List<ServiceInstance> instances = discoveryClient.getInstances("sunproduct");
            String uri = instances.get(0).getUri().toString();

            Order order = new Order();
            // 这就是让当前的Order微服务，向另外一个微服务发起http请求的代码

//            ResultVO resultVO = restTemplate.getForObject( "http://sunproduct/getProduct/" + pid, ResultVO.class);
            ResultVO resultVO = productService.findByPid(pid);
//            System.out.println("resultVO==========="+resultVO);
            Map map = (Map) resultVO.getData();
            System.out.println("map==========="+map);
            Integer stock = (Integer) map.get("stock");
            if(stock > 0) {
                // 下单
//                order.setOid(10);
                order.setPid(pid);
                order.setPname((String) map.get("pname"));
                order.setPrice((Double) map.get("price"));
                order.setNumber(number);
                order.setUid(1);
                order.setUsername("邓紫棋");
                System.out.println("order==========="+order);
                service.addOrders(order);

                // 减库存   与feign作对比
                ResultVO resultVO2 = restTemplate.getForObject("http://sunproduct/deductStock/" + pid + "/" + number, ResultVO.class);

                if(resultVO2.getStatus() == 200 ) {
                    return new ResultVO(Constants.OPER_SUCCESS, "下单成功", order);
                } else {
                    return new ResultVO(Constants.OPER_SUCCESS, "下单失败，目前库存充足，可能是product服务有问题");
                }
            } else {
                throw new RuntimeException("下单失败，库存不足");
            }
        } catch (Exception e) {
            return new ResultVO(Constants.OPER_FAILURE, e.getMessage());
        }

    }


}